package com.lynn.salary.service.impl;

import java.lang.reflect.InvocationTargetException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import com.lynn.salary.dao.ICommonDao;
import com.lynn.salary.entity.BaseEntity;
import com.lynn.salary.entity.Subject_;
import com.lynn.salary.service.IBaseEntityService;
import com.lynn.salary.util.BeansInterface;
import com.lynn.salary.util.ObjectFactory;
import com.lynn.salary.util.ToolMethod;

public abstract class BaseService implements BeansInterface,IBaseEntityService {
	protected ICommonDao dao = (ICommonDao) ObjectFactory.getTarget("dao");
	protected ThreadLocal<Connection> connection = new ThreadLocal<Connection>();

	public ICommonDao getDao() {
		return dao;
	}

	public void setDao(ICommonDao dao) {
		this.dao = dao;
	}

	public Connection getConnection() {
		return connection.get();
	}

	public void setConnection(Connection connection) {
		this.connection.set(connection);
	}

	public <T extends BaseEntity> boolean delete(String[] ids, T t) throws Exception{
		
		if(ids == null || ids.length == 0) {
			return false;
		}
		StringBuffer sql = new StringBuffer("update "+t.tableName());
		sql.append(" s");
		sql.append(" set isdeleted="+Subject_.DELETE);
		sql.append(" where s.id in ");
		sql.append("(");
		
		sql.append(ToolMethod.join(ids, ",", "'"));
		
		sql.append(");");
		dao.executeUpdateSQL(sql.toString());
		
		return true;
	}

	public <T> T save(T t) throws Exception {
		return dao.save(t);
	}

	public <T> T getByPk(String id, Class<T> t) throws Exception {
		return dao.getById(t, id);
	}

	public <T> boolean update(Map<String, Object> data, Class<T> t) throws Exception {
		
		return dao.update(t, data);
	}
	
	public <T> List<Map<String,Object>> getPartByCondition(Class<T> c,String[]part,String condition) throws Exception {
		return dao.getPartList(c, part, condition);
	}
	
	public <T extends BaseEntity> boolean addTag(String[]ids,String tag,T t) throws Exception {
		 
		StringBuffer sql = new StringBuffer("update "+t.tableName());
		sql.append(" ");
		sql.append("set "+t.tagName()+"=CONCAT("+t.tagName()+",'"+tag+"')");
		sql.append("where id in (");
		
		sql.append(ToolMethod.join(ids, ",", "'"));
		
		sql.append(") and "+t.tagName()+" not like ");
		sql.append("'%");
		sql.append(tag);
		sql.append("%'");
		
		dao.executeUpdateSQL(sql.toString());
		
		return true;
	}

	public <T extends BaseEntity> boolean removeTag(String[] ids,String tag,T t) throws Exception {
		
		StringBuffer sql = new StringBuffer("update "+t.tableName());
		sql.append(" ");
		sql.append("set "+t.tagName()+"=REPLACE("+t.tagName()+",'"+tag+"','') ");
		sql.append("where id in (");
		
		sql.append(ToolMethod.join(ids, ",", "'"));
		
		sql.append(") and "+t.tagName()+" like ");
		sql.append("'%");
		sql.append(tag);
		sql.append("%'");
		
		dao.executeUpdateSQL(sql.toString());
		
		return true;
	}

	public <T> List<Map<String, Object>> getPartByCondition(Class<T> clazz, String[] strings, String string, Object... values) throws Exception {
		return dao.getPartList(clazz, strings, string,values);
	}
	
	public void destory() {
		System.out.println("\t\t"+this.getClass().getSimpleName() + "destory completed ... ");
	}
}
